﻿using System.Web.Mvc;
using BookShop.BLL;
using BookShop.DTO;
using BookShop.Library.Common;
using BookShop.Library.Constant;
using PagedList;

namespace BookShop.Admin.Controllers
{
    public class OrderDetailController : Controller
    {
        public ActionResult Delete(long orderDetailId, long orderId)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.OrderDetailWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List", new { orderId });
            }
            var orderDetail = OrderDetailService.GetById(orderDetailId);
            var deleteResult = OrderDetailService.Delete(orderDetail);
            if (!deleteResult)
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.ErrorOccurs;
            }
            var order = OrderService.GetById(orderDetail.OrderId);
            if (order != null)
            {
                order.TotalPrice = OrderService.GetTotalPrice(order.Id);
                order = OrderService.Update(order);
                if (order == null)
                {
                    TempData[AlertMessage.TempDataKey] = AlertMessage.ErrorOccurs;
                }
            }
            return RedirectToAction("List", new { orderId });
        }

        public ActionResult List(long orderId, long? orderDetailId)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.OrderDetailRead))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("Index", "Home");
            }
            var model = OrderDetailService.GetByOrderId(orderId);
            foreach (var orderDetail in model)
            {
                orderDetail.BookTitle = (BookService.GetById(orderDetail.BookId) ?? new Book()).Title;
            }
            if (orderDetailId.HasValue)
            {
                var orderDetail = OrderDetailService.GetById(orderDetailId.Value);
                if (orderDetail != null)
                {
                    ViewBag.OrderDetailId = orderDetail.Id;
                    ViewBag.Quantity = orderDetail.Quantity;
                    ViewBag.BookId = orderDetail.BookId;
                }
            }
            ViewBag.OrderId = orderId;
            return View(model);
        }

        [HttpPost]
        public ActionResult List(long orderId, long? orderDetailId, long bookId, int quantity)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.OrderDetailRead))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("Index", "Home");
            }
            var order = OrderService.GetById(orderId);
            if (order == null)
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.NotFoundObject;
                return RedirectToAction("List", "Order");
            }
            ViewBag.OrderId = order.Id;
            if (orderDetailId.HasValue)
            {
                var orderDetail = OrderDetailService.GetById(orderDetailId.Value);
                if (orderDetail == null)
                {
                    TempData[AlertMessage.TempDataKey] = AlertMessage.NotFoundObject;
                    return RedirectToAction("List", new { orderId });
                }
                orderDetail.BookId = bookId;
                orderDetail.Quantity = quantity;
                orderDetail.TotalPrice = GetTotalPrice(bookId, quantity);
                orderDetail = OrderDetailService.Update(orderDetail);
                order.TotalPrice = OrderService.GetTotalPrice(order.Id);
                order = OrderService.Update(order);
                if (orderDetail == null || order == null)
                {
                    TempData[AlertMessage.TempDataKey] = AlertMessage.ErrorOccurs;
                }
            }
            else
            {
                var orderDetail = new OrderDetail { BookId = bookId, Quantity = quantity, OrderId = orderId };
                orderDetail = OrderDetailService.Add(orderDetail);
                if (orderDetail == null)
                {
                    TempData[AlertMessage.TempDataKey] = AlertMessage.ErrorOccurs;
                }
                else
                {
                    orderDetail.TotalPrice = GetTotalPrice(bookId, quantity); 
                    orderDetail = OrderDetailService.Update(orderDetail);
                    order.TotalPrice = OrderService.GetTotalPrice(order.Id);
                    order = OrderService.Update(order);
                    if (orderDetail == null || order == null)
                    {
                        TempData[AlertMessage.TempDataKey] = AlertMessage.ErrorOccurs;
                    }
                }
            }
            ViewBag.Quantity = quantity;
            ViewBag.BookId = bookId;
            return RedirectToAction("List", new { orderId });
        }

        public JsonResult GetAllBook()
        {
            return Json(BookService.GetAll());
        }

        private static double GetTotalPrice(long bookId, int quantity)
        {
            var book = BookService.GetById(bookId);
            if (book != null && book.IsOnSale)
            {
                book.Price = BookService.GetDiscountPrice(book.Id);
            }
            return book != null ? quantity * book.Price : 0;
        }
    }
}
